package com.xrtn.service.impl;

import com.xrtn.bean.SysUserInfo;
import com.xrtn.bean.TReplyInfo;
import com.xrtn.bean.TSubclinicInfo;
import com.xrtn.bean.TTest;
import com.xrtn.mapper.*;
import com.xrtn.service.ISysUserService;
import com.xrtn.utils.DateUtils;
import com.xrtn.utils.ObjUtils;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements ISysUserService {

    /**
     * 系统用户表
     */
    @Autowired
    private SysUserInfoMapper userMapper;
    @Autowired
    private TSubclinicInfoMapper subclinicInfoMapper;
    @Autowired
    private TReplyInfoMapper replyInfoMapper;

    @Override
    public int insertSysUser(SysUserInfo sysUserInfo) {
        String userNewNo = sysUserInfo.getUserNo();
        Map<String, Object> params = new HashMap<>();
        params.put("userNo", userNewNo);
        SysUserInfo sysUser = userMapper.selectByUserInfo(params);
        if (ObjUtils.isNotEmpty(sysUser)) {
            return -1;
        }
        userMapper.insertSelective(sysUserInfo);
        return sysUserInfo.getUserId();
    }

    @Override
    public int updateSysUser(SysUserInfo sysUserInfo) {
        if(ObjUtils.isNotEmpty(sysUserInfo.getSubclinicId())){
            Integer subclinicId = sysUserInfo.getSubclinicId();

            SysUserInfo sysUser = userMapper.selectByPrimaryKey(sysUserInfo.getUserId());

            if(subclinicId != sysUser.getSubclinicId()){

                Map<String,Object> params = new HashMap<>();
                params.put("ownerId",sysUserInfo.getUserId());
                List<TSubclinicInfo> subclinicList = subclinicInfoMapper.selectByMap(params);
                if(subclinicList.size() > 0 ){
                    sysUserInfo.setSubclinicId(null);
                    userMapper.updateByPrimaryKeySelective(sysUserInfo);
                    return -1;
                }
            }
        }
        userMapper.updateByPrimaryKeySelective(sysUserInfo);
        return sysUserInfo.getUserId();
    }

    @Override
    public List<Map<String, Object>> sysUserList(Integer status, Integer subclinicId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userStatus", status);
        params.put("subclinicId", subclinicId);
        return userMapper.selectSysUserList(params);
    }

    @Override
    public List<Map<String, Object>> sysUserList2(Integer status, Integer subclinicId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userStatus", status);
        params.put("subclinicId", subclinicId);
        return userMapper.selectSysUserList2(params);
    }

    @Override
    public SysUserInfo userDetail(int userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public List<TSubclinicInfo> sysShopList() {
        return subclinicInfoMapper.selectByMap(null);
    }

    @Override
    public int updateStatus(int userId, int userStatus) {
        Timestamp d = new Timestamp(System.currentTimeMillis());
        Map<String, Object> params = new HashMap<>();
        params.put("userStatus", userStatus);
        params.put("userId", userId);
        if (userStatus != 0) {
            params.put("leaveTime", d);
            return userMapper.updateStatusByKey(params);
        } else {
            params.put("leaveTime", null);
            return userMapper.updateStatusByKey(params);
        }
    }

    /**
     * 获取员工绩效列表
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> selectUserSaleList(Map<String, Object> params) {

        Object isMain = params.get("isMain");
        try{

            if( ObjUtils.isNotEmpty(isMain)){
                if(isMain.toString().equals("1")){
                    //月
                    String startDate = DateUtils.getMonthsFristDay(params.get("startDate").toString());
                    String endDate = DateUtils.getMonthsLastDay(params.get("startDate").toString());
                    params.put("startDate", startDate);
                    params.put("endDate", endDate);
                    params.put("workDay", DateUtils.getDayDifference(DateUtils.parse(startDate,DateUtils.YYYY_MM_DD),
                            DateUtils.parse(endDate,DateUtils.YYYY_MM_DD)));
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return userMapper.selectUserSaleList(params);
    }

    /**
     * 获取员工绩效明细
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> selectUserSaleDetail(Map<String, Object> params) {
        return userMapper.selectUserSaleDetail(params);
    }

    /**
     * 获取员工绩效明细
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> selectUserReplyList(Map<String, Object> params) {
        return replyInfoMapper.selectUserReplyList(params);
    }

    /**
     * 获取评价列表
     *
     * @param replyId
     * @return
     */
    @Override
    public TReplyInfo selectUserReplyInfo(Integer replyId) {
        return replyInfoMapper.selectByPrimaryKey(replyId);
    }

    /**
     * 修改评价详情
     *
     * @param replyInfo
     * @return
     */
    @Override
    public Integer updateUserReplyInfo(TReplyInfo replyInfo) {

        TReplyInfo reply = replyInfoMapper.selectByPrimaryKey(replyInfo.getReplyId());

        if(ObjUtils.isNotEmpty(reply)){
            return replyInfoMapper.updateByPrimaryKeySelective(replyInfo);
        }

        return 0;
    }
}
